package 
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.ColorTransform;
	import flash.system.System;
	import flash.utils.Timer;
	import nl.demonsters.debugger.MonsterDebugger;
	
	/**
	 * ...
	 * @author Harry
	 */
	public class Main_ extends Sprite 
	{
		private var _destroyOjbectArr:Array = new Array();
		private var _timer:Timer;
		
		public function Main_():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.addEventListener(MouseEvent.CLICK, _onMouseClick, false, 0, true);
			trace(this, Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb');
			MonsterDebugger.trace(this, stage);
		}
		
		private function _onMouseClick(e:MouseEvent):void 
		{
			for ( var __i:int = 0; __i < 10000; __i++)
			{
				_createShape();
			}
			trace(this, Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb');
			
			_timer = new Timer(1000, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, _onTimerComplete, false, 0, true);
			_timer.start();
		}
		
		private function _onTimerComplete(e:TimerEvent):void 
		{
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, _onTimerComplete);
			trace(this, Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb');
			trace(this, this.numChildren);
			//while (this.numChildren)
			//{
				//var __o:DisplayObject = this.getChildAt(0);
				//this.removeChild(__o);
				//__o = null;
			//}
			
			for ( var __i:int = 0; __i < _destroyOjbectArr.length; __i++)
			{
				this.removeChild(_destroyOjbectArr[__i]);
				_destroyOjbectArr[__i] = null;
				//delete(_destroyOjbectArr[__i]);
			} 
			while (_destroyOjbectArr.length)
			{
				_destroyOjbectArr.pop();
			}
			trace(this, Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb');
			trace(this, this.numChildren);
			MonsterDebugger.trace(this, stage);
			
			//this.addEventListener(Event.ENTER_FRAME, _onEnterFrame, false, 0, true);
		}
		
		private function _onEnterFrame(e:Event):void 
		{
			trace(this, Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb');
		}
		
		private function _createShape():void 
		{
			// creates a red square
			var square:Shape = new Shape();
			square.graphics.beginFill(0x000000);
			square.graphics.drawRect(0, 0, 100, 100);

			//var colorTransform:ColorTransform = square.transform.colorTransform;
			//colorTransform.color = 0xFF0000;
			//square.transform.colorTransform = colorTransform;

			this.addChild(square);
			
			_destroyOjbectArr.push(square);
		}
		
	}
	
}